package com.itguigu.fruit.view;

import com.itguigu.fruit.pojo.Fruit;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class showMainMenu {
    List<Fruit> fruitList = new ArrayList<>();
    Scanner input = new Scanner(System.in);

    {
        try {
            loadData();
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }


    public void loadData() throws IOException, ClassNotFoundException {


//        InputStream input= this.getClass().getClassLoader().getResourceAsStream("D:/itguigu/pro-fruit/pro01-fruit/src/resources/fruit.dat");
        File file = new File("D:/itguigu/pro-fruit/pro02-fruit/src/resources/fruit.dat");
        InputStream input = new FileInputStream(file);
        //正常读取
//        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
////        InputStreamReader
//        String str = null;
//        while ((str = reader.readLine()) != null) {
//            String[] arr = str.split(" ");
//            String fname = arr[0];
//            Integer price = Integer.parseInt(arr[1]);
//            Integer fcount = Integer.parseInt(arr[2]);
//            String remark = arr[3];
//
//
//            Fruit fruit = new Fruit(fname, price, fcount, remark);
//            fruitList.add(fruit);
//
//        }
        //序列化
        if(input!=null){
            ObjectInputStream ois=new ObjectInputStream(input);
            fruitList=(List<Fruit>)ois.readObject();
        }
    }

    //显示查询库存列表
    public void showSelete() {
        System.out.println("-------------------------");

        System.out.println("名称\t\t单价\t\t库存\t\t备注");
        if (fruitList == null || fruitList.size() <= 0) {
            System.out.println("对不起，当前没有库存！");
        } else {
            fruitList.forEach(System.out::println);
        }

        System.out.println("-------------------------");
    }

    public int show() {
        System.out.println("------欢迎使用水果片存系统-----");
        System.out.println("1.显示库存列表");
        System.out.println("2.添加库存记承");
        System.out.println("3.查看特定库存");
        System.out.println("4.水果下架");
        System.out.println("5.退出");
        System.out.println("----------------------------");
        System.out.print("请选择：");


        return input.nextInt();
    }

    //    根据名称查询指定的库存记录
    public Fruit fruitByFname(String fname) {
        return fruitList.stream().filter(fruit -> fname.equals(fruit.getFname())).findFirst().orElseGet(() -> null);
//        for (Fruit fruit : fruitList){
//            if (fname.equals(fruit.getFname())){
//                return fruit;
//            }
//        }
//        return null;
    }

    public void addFruit() {
        System.out.print("请输入水果名称：");
        String fname = input.next();
        Fruit fruit = fruitByFname(fname);
        if (fruit == null) {
            System.out.print("请输入水果单价：");
            Integer price = input.nextInt();
            System.out.print("请输入水果库存：");
            Integer fcount = input.nextInt();
            System.out.print("请输入水果备注：");
            String remark = input.next();

            fruit = new Fruit(fname, price, fcount, remark);
            fruitList.add(fruit);

        } else {
            System.out.print("请输入你要追加的商品库存：");
            int i = input.nextInt();
            fruit.setFcount(fruit.getFcount() + i);
        }
    }


    //查看特定库存
    public void showFruitInfo() {
        System.out.print("请输入水果名称：");
        String fname = input.next();
        Fruit fruit = fruitByFname(fname);
        if (fruit == null) {
            System.out.println("-------------------");
            System.out.println("当前水果没有库存");
            System.out.println("-------------------");
        } else {
            System.out.println("-------------------");
            System.out.println("名称\t\t单价\t\t库存\t\t备注");
            System.out.println(fruit);
            System.out.println("-------------------");
        }
    }

    //下架指定水果
    public void  delFruit(){
        System.out.print("请输入水果名称：");
        String fname = input.next();
        Fruit fruit = fruitByFname(fname);
        if (fruit == null) {
            System.out.println("-------------------");
            System.out.println("没有当前水果");
            System.out.println("-------------------");
        } else {
            System.out.println("请确认是否删除？（Y/N）");
//            System.out.println("名称\t\t单价\t\t库存\t\t备注");
            String comfirm=input.next();
            if("y".equalsIgnoreCase(comfirm)){
                fruitList.remove(fruit);
                System.out.println("删除成功");
            }
//            System.out.println(fruit);

        }
    }

    public boolean exit(){
        System.out.print("是否退出？（y/n）:");
        String comfirm=input.next();

        boolean flag= !"y".equalsIgnoreCase(comfirm);
        if(!flag){
            try {
                saveFruit();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return flag;
    }

    public void saveFruit() throws IOException {
        File file = new File("pro02-fruit/src/resources/fruit.dat");
//        System.out.println(file.exists());
//        PrintWriter pw= new PrintWriter(new FileWriter(file));
//        fruitList.forEach(fruit -> pw.println(fruit.getFname()+ " "+fruit.getPrice()+" "+fruit.getFcount()+" "+fruit.getRemark()));
//        pw.flush();
        ObjectOutputStream pw = new ObjectOutputStream(new FileOutputStream(file));
        pw.writeObject(fruitList);
        pw.flush();



    }

}






